﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FmcWebsite.Core;

namespace ContentManagementAdministrator.Dialogs
{
    public partial class FormArticleImagesEdit : Form
    {
        private Article article;
        private IEnumerable<CropSize> all_crop = CropSize.GetAll();

        public FormArticleImagesEdit()
        {
            InitializeComponent();
            comboBoxCrop.Items.AddRange(all_crop.ToArray());
            comboBoxCrop.SelectedIndex = 1;
        }

        private void SetArticle(Article article)
        {
            this.article = article;
            imageList1.Images.Clear();
            listView1.Items.Clear();
            foreach (AttachmentIMAGE img in article.Images)
            {
                imageList1.Images.Add(Thumbnailer.CreateThumbnail(img.Image, imageList1.ImageSize));
                var item = listView1.Items.Add(img.Name);
                item.ImageIndex = imageList1.Images.Count - 1;
                item.Tag = img;
            }

            if ((article.Category != null) && (article.Category.Code == "SLID"))
                comboBoxCrop.SelectedIndex = 2;
        }

        static public DialogResult Execute(Article article)
        {
            FormArticleImagesEdit form = new FormArticleImagesEdit();
            form.SetArticle(article);
            return form.ShowDialog();
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count>0)
            {
                if (MessageBox.Show("Are you sure you want to delete this image?", "Warning", MessageBoxButtons.YesNo,  MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    foreach (ListViewItem item in listView1.SelectedItems)
                    {
                        AttachmentIMAGE img = (AttachmentIMAGE)item.Tag;
                        int idx = item.ImageIndex;
                        img.Delete();
                        listView1.Items.Remove(item);
                        imageList1.Images.RemoveAt(idx);
                    }
                }
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            openFileDialogImages.FileName = "*.jpg;*.png;*.gif";
            if (openFileDialogImages.ShowDialog() == DialogResult.OK)
            {
                var crop = (CropSize)comboBoxCrop.SelectedItem;
                foreach (string filename in openFileDialogImages.FileNames)
                {                    
                    var img = article.AddImage(filename, (new FileInfo(filename)).Name, crop.Size);
                    imageList1.Images.Add(Thumbnailer.CreateThumbnail(img.Image, imageList1.ImageSize));
                    var item = listView1.Items.Add(img.Name);
                    item.ImageIndex = imageList1.Images.Count - 1;
                    item.Tag = img;
                }
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                var item = (AttachmentIMAGE)listView1.SelectedItems[0].Tag;
                pictureBox1.Image = item.Image;
            }
            else
                pictureBox1.Image = null;
        }
    }
}
